

#include "widget_funca_smart_chat.h"
#include "ui_widget_funca_smart_chat.h"

Widget_funcA_Smart_chat::Widget_funcA_Smart_chat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcA_Smart_chat)
{
    this->manager = new AiServerManager();
    connect(this->manager, &AiServerManager::resposeReceive, this, &Widget_funcA_Smart_chat::onAiResponseReceived);
    connect(this->manager, &AiServerManager::errorOccured, this, &Widget_funcA_Smart_chat::onAiErrorOccurred);
    ui->setupUi(this);
    
    messageLabel = new QLabel(this);
    // 可以先设置一些基础样式
    messageLabel->setWordWrap(true); // 自动换行
    messageLabel->setContentsMargins(10, 10, 10, 10); // 内边距
    // 设定按钮大小
    ui->Button_send->setFixedSize(80 , 30);
    

    // 设置背景透明
    ui->textEdit->setStyleSheet("background: transparent;");
    // 设置透明背景并去除边框
    ui->textEdit->setStyleSheet("background: transparent; border: none;");
    // 确保文本框能够正常输入
    ui->textEdit->setAttribute(Qt::WA_OpaquePaintEvent, false);
    // 设置字体
    ui->textEdit->setFont(QFont("楷体" , 13));
    
    
    // 设定消息对话框为垂直布局
    Message_layout = new QVBoxLayout(ui->widget_show);
    
    // 安装间隔器件
//    message_Spacer = new QSpacerItem( 1000 , 20, QSizePolicy::Maximum , QSizePolicy::Expanding);
//    Message_layout->addItem(message_Spacer);
//    qDebug() << Message_layout->count();
    // 连接发射信号和槽函数
    connect(ui->Button_send , &Button_Cust_Base::signals_click , this , &Widget_funcA_Smart_chat::Slota_Add_Send_Message);

}

Widget_funcA_Smart_chat::~Widget_funcA_Smart_chat()
{
    delete ui;
}

void Widget_funcA_Smart_chat::set_Send_Layout()
{
    // 设置发送消息的样式，比如背景色为蓝色，文字白色，靠右对齐等
    this->setStyleSheet("background-color: #409EFF; color: white; border-radius: 8px;");
    messageLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
}

void Widget_funcA_Smart_chat::set_Receive_Layout()
{
    // 设置接收消息的样式，比如背景色为浅灰色，文字黑色，靠左对齐等
    this->setStyleSheet("background-color: #F5F7FA; color: black; border-radius: 8px;");
    messageLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
}

void Widget_funcA_Smart_chat::set_Edit_Text(const QString &text)
{
     messageLabel->setText(text);
}


// 修改发送消息的槽函数
void Widget_funcA_Smart_chat::Slota_Add_Send_Message()
{
    QString userMessage = ui->textEdit->toPlainText().trimmed();
    if(!userMessage.isEmpty())
    {
        // 创建并显示用户消息
        chat_message = new Widget_smart_chat_message(ui->widget_show);
        Message_layout->addWidget(chat_message);
        chat_message->set_Send_Layout();
        chat_message->show();
        chat_message->set_Edit_Text(userMessage);

        // 清空输入框
        ui->textEdit->clear();

        // 禁用发送按钮，防止重复发送
        ui->Button_send->setEnabled(false);

        // 显示"思考中..."消息
        Widget_smart_chat_message *thinkingMsg = new Widget_smart_chat_message(ui->widget_show);
        Message_layout->addWidget(thinkingMsg);
        this->set_Receive_Layout();
        thinkingMsg->set_Edit_Text("思考中...");
        thinkingMsg->show();

        // 发送消息给AI
        manager->sendMassge(userMessage);
    }
    this->update();
}

// 处理AI回复
void Widget_funcA_Smart_chat::onAiResponseReceived(const QString &response)
{
    // 重新启用发送按钮
    ui->Button_send->setEnabled(true);

    // 移除"思考中..."消息（需要记录或查找）
    // 这里简化处理，实际可能需要更复杂的管理

    // 创建并显示AI回复
    Widget_smart_chat_message *aiMessage = new Widget_smart_chat_message(ui->widget_show);
    Message_layout->addWidget(aiMessage);
    this->set_Send_Layout(); // 接收布局（左侧显示）
    aiMessage->set_Edit_Text(response);
    aiMessage->show();

    // 滚动到底部
    QScrollArea *scrollArea = qobject_cast<QScrollArea*>(ui->widget_show->parentWidget());
    if (scrollArea)
    {
        scrollArea->verticalScrollBar()->setValue(scrollArea->verticalScrollBar()->maximum());
    }
}

// 处理AI错误
void Widget_funcA_Smart_chat::onAiErrorOccurred(const QString &error)
{
    ui->Button_send->setEnabled(true);
    // 显示错误消息
    QMessageBox::warning(this,"提示",error, QMessageBox::Ok);
}
